var ds_role = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: 'role_Json_list.action',
        method: 'POST'
    }),
    reader: new Ext.data.JsonReader({
        root: 'roleList',
        totalProperty: 'total'
    }, [{
        name: 'roleId',
        mapping: 'roleId',
        type: 'long'
    }, {
        name: 'roleName',
        mapping: 'roleName',
        type: 'string'
    }, {
        name: 'homePage',
        mapping: 'homePage',
        type: 'string'
    }, {
        name: 'description',
        mapping: 'description',
        type: 'string'
    }, {
        name: 'operation',
        type: 'string'
    }])
});

var cm_role = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
    header: '角色名称',
    dataIndex: 'roleName',
    width: 140,
    sortable: true,
    renderer: function(value, metadata, record){
        return "<a href = 'javascript:void(0)' onclick=javascript:roleShow('" + record.data.roleId + "')>" + value + "</a>";
    }
}, {
    header: '角色主页',
    dataIndex: 'homePage',
    width: 200,
    sortable: true
}, {
    header: '角色描述',
    width: 120,
    dataIndex: 'description',
    sortable: true
}, {
    header: "操作",
    width: 120,
    dataIndex: 'operation',
    renderer: function(value, metadata, record){
        return "<a href = 'javascript:void(0)' onclick=javascript:roleShow('" + record.data.roleId + "')>查看</a>&nbsp;&nbsp;"
            	+"<a href = 'javascript:void(0)' onclick=javascript:roleModity('"+record.data.roleId+"')>编辑</a>&nbsp;&nbsp;"
//           	 	+"<a href = 'javascript:void(0)' onclick=javascript:del('"+record.data.roleId+"')>删除 </a>";
    }
}]);

var role_add_button = new Ext.Button({
    text: '添加角色',
    hidden: true,
    width: 100,
    iconCls: 'role_add',
    handler: function(){
        var tab_role = new Ext.TabPanel({
            title: '权限选择',
            id: 'tab_role',
            plain: true,
            frame: true,
            minTabWidth: 20,
            height: 150,
            autoScroll: true,
            cls: "clear_l",
            enableTabScroll: true
        });
        Ext.Ajax.request({
            url: 'role_Show_Json_view.action',
            method: 'post',
            success: function(resp, opts){
                var formPanel = new Array();
                var respText = Ext.util.JSON.decode(resp.responseText);
                if (respText.success) {
                    operationGroupList = respText.operationGroupList;
                    for (var i = 0; i < operationGroupList.length; i++) {
                        var operationCheckbox = new Array();
                        for (var j = 0; j < operationGroupList[i].operations.length; j++) {
                            operationCheckbox[j] = new Ext.form.Checkbox({
                                boxLabel: '<span class="cut_label" ext:qtip=' + operationGroupList[i].operations[j].operationName + '>' + operationGroupList[i].operations[j].operationName + '</span>',
                                name: 'operationIds',
                                width: 180,
                                cls: 'cut_checkbox',
                                inputValue: operationGroupList[i].operations[j].operationId
                            });
                        }
                        var checkbox_form = {
                            title: operationGroupList[i].operationGroupName,
                            layout: 'column',
                            labelAlign: 'right',
                            autoWidth: true,
                            autoHeight: true,
                            closable: false,
                            border: false,
                            labelWidth: 140,
                            items: operationCheckbox
                        };
                        tab_role.add(checkbox_form);
                    }
                    tab_role.setActiveTab(0);
                }
                else {
                    Ext.Msg.show({
                        title: '错误提示',
                        msg: '出初化失败!',
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR,
                        width: 200
                    })
                }
            },
            failure: function(){
                Ext.Msg.show({
                    title: '错误提示',
                    msg: '初始化失败!',
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR,
                    width: 200
                });
            }
        });
        var form_role = new Ext.form.FormPanel({
            id: 'role_from',
            labelAlign: 'right',
            labelWidth: 80,
            frame: true,
            region: 'center',
            items: [{
                xtype: 'textfield',
                name: 'role.roleName',
                allowBlank: false,
                fieldLabel: '角色名称',
                itemCls: 'form_long'
            }, {
                xtype: 'textfield',
                name: 'role.homePage',
                allowBlank: true,
                fieldLabel: '角色主页',
                itemCls: 'form_long clear'
            
            }, {
                xtype: 'textarea',
                name: 'role.description',
                allowBlank: true,
                fieldLabel: '角色描述',
                itemCls: 'clear',
                width: '410',
                height: '80'
            }, tab_role],
            buttons: [{
                text: '添加',
                type: 'submit',
                handler: function(){
                    form_role.form.doAction('submit', {
                        url: 'role_add.action',
                        method: 'post',
                        success: function(form, action){
                            if (action.result.success) {
                                Ext.Msg.show({
                                    title: '操作提示',
                                    msg: '添加成功!',
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.INFO,
                                    width: 200
                                });
                                win_add.hide();
                                ds_role.reload();
                            }
                            else {
                                Ext.Msg.show({
                                    title: '错误提示',
                                    msg: action.result.errorMessage,
                                    buttons: Ext.Msg.OK,
                                    icon: Ext.Msg.ERROR,
                                    width: 200
                                });
                            }
                        },
                        failure: function(from, action){
                            Ext.Msg.show({
                                title: '错误提示',
                                msg: '添加失败',
                                buttons: Ext.Msg.OK,
                                icon: Ext.Msg.ERROR,
                                width: 200
                            });
                        }
                    });
                }
            }, {
                text: '重置',
                handler: function(){
                    form_role.form.reset();
                }
            }, {
                text: '关闭',
                handler: function(){
                    win_add.close();
                }
            }]
        });
        var win_add = new Ext.Window({
            title: '添加角色',
            width: 600,
            constrain: true,
            iconCls: 'role_add',
            resizable: false,
            constrain: true,
            autoHeight: true,
            modal: true,
            closeAction: 'hide',
            closable: false,
            items: form_role
        }).show();
    }
});

window.roleShow = function(roleId){
    Ext.Ajax.request({
        url: 'role_Show_Json_view.action',
        method: 'post',
        params: {
            roleId: roleId
        },
        success: function(resp, opts){
            var respText = Ext.util.JSON.decode(resp.responseText);
            if (respText.success) {
                var operationIds = respText.roleOperation;
                var operationCheckbox = new Array();
                
                for (var k = 0; k < operationIds.length; k++) {
                    operationCheckbox[k] = {
                        xtype: 'label',
                        labelWidth: 120,
                        html: '<span class="cut_label" ext:qtip=' + operationIds[k].operationName + '>' + operationIds[k].operationName + '</span>',
                        cls: 'role_label'
                    }
                };
                var checkbox_panel = new Ext.Panel({
                    title: '操作权限',
                    layout: 'column',
                    labelAlign: 'right',
                    width: 520,
                    height: 200,
                    autoScroll: true,
                    frame: true,
                    closable: false,
                    labelWidth: 120,
                    items: [operationCheckbox],
                    itemCls: 'unedit'
                });
                form_role = new Ext.form.FormPanel({
                    id: 'role_from',
                    labelAlign: 'right',
                    labelWidth: 80,
                    frame: true,
                    items: [{
                        xtype: 'textfield',
                        name: 'role.roleName',
                        readOnly: true,
                        fieldLabel: '角色名称',
                        value: respText.role.roleName,
                        width: 400,
                        itemCls: 'unedit'
                    }, {
                        xtype: 'textfield',
                        name: 'role.homePage',
                        readOnly: true,
                        fieldLabel: '角色主页',
                        value: respText.role.homePage,
                        width: 400,
                        itemCls: 'unedit'
                    }, {
                        xtype: 'textfield',
                        name: 'role.description',
                        readOnly: true,
                        fieldLabel: '角色描述',
                        value: respText.role.description,
                        width: '400',
                        autoHeight: true,
                        itemCls: 'unedit'
                    }, checkbox_panel, {
                        xtype: 'textfield',
                        name: 'role.roleId',
                        value: respText.role.roleId,
                        width: 400,
                        hidden: true,
                        itemCls: 'unedit'
                    }],
                    buttons: [{
                        text: '关闭',
                        handler: function(){
                            win_show.hide();
                        }
                    }]
                });
                var win_show = new Ext.Window({
                    title: '查看角色',
                    width: 550,
                    resizable: false,
                    constrain: true,
                    iconCls: 'role_manager',
                    constrain: true,
                    autoHeight: true,
                    modal: true,
                    closeAction: 'hide',
                    closable: false,
                    items: form_role
                }).show();
            }
            else {
                Ext.Msg.show({
                    title: '错误提示',
                    msg: '出初化失败!',
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR,
                    width: 200
                })
            }
        },
        failure: function(){
            Ext.Msg.show({
                title: '错误提示',
                msg: '出初化失败!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR,
                width: 200
            });
        }
    });
}

window.roleModity = function(roleId){
    var form_role = '';
    var tab_role = new Ext.TabPanel({
        title: '权限选择',
        id: 'tab_role',
        plain: true,
        minTabWidth: 20,
        frame: true,
        height: 150,
        autoScroll: true,
        cls: "clear_l",
        enableTabScroll: true
    });
    Ext.Ajax.request({
        url: 'role_Show_Json_view.action',
        method: 'post',
        params: {
            roleId: roleId
        },
        success: function(resp, opts){
            var respText = Ext.util.JSON.decode(resp.responseText);
            if (respText.success) {
                var operationGroupList = respText.operationGroupList;
                var operationIds = respText.roleOperation;
                var checkbox_form = new Array();
                for (var i = 0; i < operationGroupList.length; i++) {
                var operationCheckbox = new Array();
                    for (var j = 0; j < operationGroupList[i].operations.length; j++) {
                    	var temp = 'check'+i+j;
                        operationCheckbox[j] = new Ext.form.Checkbox({
                            boxLabel: '<span class="cut_label" ext:qtip=' + operationGroupList[i].operations[j].operationName + '>' + operationGroupList[i].operations[j].operationName + '</span>',
                            width: 180,
                            id: temp,
                            cls: 'cut_checkbox',
                            inputValue: operationGroupList[i].operations[j].operationId
                        });
                        for (var k = 0; k < operationIds.length; k++) {
                            if (operationIds[k].operationId == operationGroupList[i].operations[j].operationId) {
                                operationCheckbox[j].checked = true;
                                break;
                            }
                        };
                                            }
                    checkbox_form[i] = new Ext.Panel({
                        title: operationGroupList[i].operationGroupName,
                        layout: 'column',
                        labelAlign: 'right',
                        autoWidth: true,
                        autoHeight: true,
                        closable: false,
                        border: false,
                        labelWidth: 140,
                        items: operationCheckbox
                    });
                    tab_role.add(checkbox_form[i]);
                }
                	tab_role.setActiveTab(0);
                form_role = new Ext.form.FormPanel({
                    id: 'role_from',
                    labelAlign: 'right',
                    labelWidth: 80,
                    frame: true,
                    items: [{
                        xtype: 'textfield',
                        name: 'role.roleName',
                        allowBlank: false,
                        fieldLabel: '角色名称',
                        value: respText.role.roleName,
                        itemCls: 'form_long'
                    }, {
                        xtype: 'textfield',
                        name: 'role.homePage',
                        allowBlank: true,
                        fieldLabel: '角色主页',
                        value: respText.role.homePage,
                        itemCls: 'form_long'
                    }, {
                        xtype: 'textarea',
                        name: 'role.description',
                        allowBlank: true,
                        fieldLabel: '角色描述',
                        value: respText.role.description,
                        itemCls: 'clear',
                        width: '410',
                        height: '120'
                    }, tab_role, {
                        xtype: 'textfield',
                        name: 'role.roleId',
                        value: respText.role.roleId,
                        itemCls: 'form_text',
                        hidden: true
                    }],
                    buttons: [{
                        text: '修改',
                        type: 'submit',
                        handler: function(){
                        	var operationIds = new Array();
                        	for (var i = 0; i < operationGroupList.length; i++) {
                        		for (var j = 0; j < operationGroupList[i].operations.length; j++) {
                        			var temp = 'check'+i+j;
                        			if(Ext.getCmp(temp).checked){
                        				operationIds.push(Ext.getCmp(temp).inputValue);
                        			}
                        		}
                        	}
                            form_role.form.doAction('submit', {
                                url: 'role_Modify_Json_edit.action',
                                method: 'post',
                                params:{operationIds:operationIds},
                                success: function(form, action){
                                    if (action.result.success) {
                                        Ext.Msg.show({
                                            title: '操作提示',
                                            msg: '修改成功!',
                                            buttons: Ext.Msg.OK,
                                            icon: Ext.Msg.INFO,
                                            width: 200
                                        });
                                        win_modify.hide();
                                        ds_role.reload();
                                    }
                                    else {
                                        Ext.Msg.show({
                                            title: '错误提示',
                                            msg: action.result.errorMessage,
                                            buttons: Ext.Msg.OK,
                                            icon: Ext.Msg.ERROR,
                                            width: 200
                                        });
                                    }
                                },
                                failure: function(from, action){
                                    Ext.Msg.show({
                                        title: '错误提示',
                                        msg: '修改失败',
                                        buttons: Ext.Msg.OK,
                                        icon: Ext.Msg.ERROR,
                                        width: 200
                                    });
                                }
                            });
                        }
                    }, {
                        text: '重置',
                        handler: function(){
                            form_role.form.reset();
                        }
                    }, {
                        text: '关闭',
                        handler: function(){
                            win_modify.hide();
                        }
                    }]
                });
                var win_modify = new Ext.Window({
                    title: '修改权限',
                    width: 600,
                    iconCls: 'role_edit',
                    constrain: true,
                    resizable: false,
                    autoHeight: true,
                    constrain: true,
                    modal: true,
                    closeAction: 'hide',
                    closable: false,
                    items: form_role
                }).show();
            }
            else {
                Ext.Msg.show({
                    title: '错误提示',
                    msg: '出初化失败!',
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR,
                    width: 200
                })
            }
        },
        failure: function(){
            Ext.Msg.show({
                title: '错误提示',
                msg: '出初化失败!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR,
                width: 200
            });
        }
    });
}

window.del = function(actionUrl){
    Ext.Msg.confirm('删除确认', '你确定删除?', function(btn){
        if (btn == 'yes') {
            Ext.Ajax.request({
                url: "role_delete.action",
                method: 'post',
                params: {
                    roleId: actionUrl
                },
                success: function(resp, opts){
                    var respText = Ext.util.JSON.decode(resp.responseText);
                    if (respText.success) {
                        Ext.Msg.show({
                            title: '操作提示',
                            msg: '删除成功!',
                            buttons: Ext.Msg.OK,
                            icon: Ext.Msg.INFO,
                            width: 200
                        });
                        ds_role.reload();
                    }
                    else {
                        Ext.Msg.show({
                            title: '错误提示',
                            msg: '删除失败!',
                            buttons: Ext.Msg.OK,
                            icon: Ext.Msg.ERROR,
                            width: 200
                        });
                    }
                },
                failure: function(){
                    Ext.Msg.show({
                        title: '错误提示',
                        msg: '删除失败!',
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR,
                        width: 200
                    });
                }
            });
        }
    });
}

var role_button = new Ext.Button({
    text: '导入角色',
    hidden: true,
    width: 100,
    iconCls: 'role_in',
    handler: function(){
        Ext.Msg.confirm('确认导入', '你确定从XML中导入角色?', function(btn){
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: 'role_Reset.action',
                    method: 'post',
                    success: function(resp, opts){
                        var respText = Ext.util.JSON.decode(resp.responseText);
                        if (respText.success) {
                            Ext.Msg.show({
                                title: '操作提示',
                                msg: '导入成功',
                                buttons: Ext.Msg.OK,
                                icon: Ext.Msg.INFO,
                                width: 200
                            });
                            ds_role.load({
                                params: {
                                    start: 0,
                                    limit: 20
                                }
                            });
                        }
                        else {
                            Ext.Msg.show({
                                title: '错误提示',
                                msg: '导入失败!',
                                buttons: Ext.Msg.OK,
                                icon: Ext.Msg.ERROR,
                                width: 200
                            })
                        }
                    },
                    failure: function(){
                        Ext.Msg.show({
                            title: '错误提示',
                            msg: '导入失败!',
                            buttons: Ext.Msg.OK,
                            icon: Ext.Msg.ERROR,
                            width: 200
                        });
                    }
                });
            }
        });
    }
});

var grid_role = new Ext.grid.GridPanel({
    title: '当前位置：角色管理',
    loadMask: {
        msg: '数据加载中...'
    },
    ds: ds_role,
    store: ds_role,
    region: 'center',
    autoScroll: true,
    cm: cm_role,
    autoExpandColumn: 3,
    tbar: [role_add_button, role_button],
    bbar: new Ext.PagingToolbar({
        pageSize: 20,
        store: ds_role,
        displayInfo: true,
        beforePageText: '第',
        afterPageText: '页-共{0}页',
        displayMsg: '显示{0} - {1} 条  共 {2} 条',
        emptyMsg: "没有记录"
    })
});

ds_role.load({
    params: {
        start: 0,
        limit: 20
    }
});

var l_role = {
    id: 'role-search',
    border: false,
    layout: 'border',
    items: [grid_role]
};

Ext.getCmp('content-panel').add(l_role);
Ext.getCmp('content-panel').layout.setActiveItem('role-search');
